#include "RefObj.h"

namespace BASE{
    volatile int RefObj::sAllCount = 0;

    int RefObj::addRef() {
        __gnu_cxx::__atomic_add(&mRefCount, 1);
        __gnu_cxx::__atomic_add(&sAllCount, 1);
        return mRefCount;
    }
    
    bool RefObj::release() {
        __sync_sub_and_fetch(&sAllCount, 1);

        if (__sync_sub_and_fetch(&mRefCount, 1) <= 0) {
            delete this;
            return true;
        }
        return false;
    }   
}
